package com.fastadmin.mapper.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.fastadmin.pojo.FastParam;
import com.fastadmin.pojo.User;
import com.xphsc.easyjdbc.EasyJdbcTemplate;
import com.xphsc.easyjdbc.page.PageInfo;
import org.asynchttpclient.util.StringBuilderPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Repository
public class UserMapper  {

    @Autowired
    EasyJdbcTemplate easyJdbcTemplate;

    public User findByUsername(String username) {
        StringBuilder sql = StringBuilderPool.DEFAULT.stringBuilder();
        sql.append(" SELECT ");
        sql.append(" 	* ");
        sql.append(" FROM ");
        sql.append(" 	t_user ");
        sql.append(" WHERE ");
        sql.append(" 	username = ? ");
        return easyJdbcTemplate.get(sql.toString(), User.class, username);
    }

    public List<User> findAll() {
        StringBuilder sql = StringBuilderPool.DEFAULT.stringBuilder();
        sql.append(" SELECT ");
        sql.append(" 	* ");
        sql.append(" FROM ");
        sql.append(" 	t_user ");
        return easyJdbcTemplate.find(sql.toString(), User.class);
    }

    public int updateByPrimaryKeySelective(User user) {
        int update = this.easyJdbcTemplate.update(user);
        return update;
    }

    public List<Map<String, Object>> findRolesNameByUserName(String username) {
        StringBuilder sql = StringBuilderPool.DEFAULT.stringBuilder();
        sql.append(" SELECT ");
        sql.append(" 	t_role.role_key ");
        sql.append(" FROM ");
        sql.append(" 	t_role, ");
        sql.append(" 	t_user_role, ");
        sql.append(" 	t_user ");
        sql.append(" WHERE 1=1 ");
        sql.append(" AND	t_role.id = t_user_role.role_id ");
        sql.append(" AND t_user.id = t_user_role.user_id ");
        sql.append(" AND t_user.username=? ");
        return this.easyJdbcTemplate.find(sql.toString(),username);


    }

    /**
     * 根据
     *
     * @param fastParam
     * @return
     */
    public PageInfo<User> findByFastParam(FastParam fastParam) {
        String sql = fastParam.getSql("t_user");
        PageInfo<User> pageInfo = this.easyJdbcTemplate.findByPage(sql, User.class, fastParam.offset, fastParam.limit);
        return pageInfo;
    }


    public Object saveAndGetKey(User user) {
        user.setCreate_time(new Timestamp(System.currentTimeMillis()));
        user.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        return this.easyJdbcTemplate.insertKey(user);
    }

    public void deleteByIds(String ids) {
        String[] split = StrUtil.split(ids, ",");
        this.easyJdbcTemplate.deleteByIds(User.class, CollUtil.toList(split));
    }

    public User findById(Object id) {
        return this.easyJdbcTemplate.getByPrimaryKey(User.class, Convert.toStr(id));
    }
}
